package one.stand.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import one.stand.aop.ApiController;
import one.stand.entity.excel.CustomerExcelEntity;
import one.stand.enums.ResultEnum;
import one.stand.model.ResultModel;
import one.stand.request.*;
import one.stand.response.*;
import one.stand.util.ExcelUtil;
import one.stand.util.ExportDataDTO;
import one.stand.util.ImportUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: ZRH
 * @CreateDate: 2019/4/17 9:45
 */
@RestController
@RequestMapping("crmPc/customer")
@Api(description = "crm-PC客户管理",tags="crm-PC客户管理")
@Slf4j
public class CustomerController extends BaseController{

    @ApiOperation(value = "获取公司员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "name", value = "名字或电话查询", paramType = "String"),
    })
    @PostMapping("getCompanyStaffList")
    @ApiController(isLogin = true,company=true,card = true,paramClass = BusinessCardRequest.class)
    public ResultModel<Map<String, Object>> getCompanyStaffList(@RequestAttribute BusinessCardRequest request) {
        
        return crmFegin.getCompanyStaffList(request);
    }

    @ApiOperation(value = "存为客户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerState", value = "1:独占客户 2:共享客户 3:成单客户", required = true, paramType = "Long"),
            @ApiImplicitParam(name = "lableIds", value = "标签id", required = true, paramType = "Long[]"),
            @ApiImplicitParam(name = "cardId", value = "名片id", required = true, paramType = "Long"),
    })
    @ApiController(isLogin = true, company = true,card = true,paramClass = CustomerRequest.class)
    @PostMapping("addCustomer")
    public ResultModel<Boolean> addCustomer(@RequestAttribute CustomerRequest request) {
        
        return crmFegin.addCustomer(request);
    }

    @ApiOperation(value = "客户类型分组统计")
    @ApiController(isLogin = true, company = true,card = true,paramClass = CustomerRequest.class)
    @PostMapping("countCustomer")
    public ResultModel<List<CustomerResponse>> countCustomer(@RequestAttribute CustomerRequest request) {
        
        return crmFegin.countCustomer(request);
    }

    @ApiOperation(value = "客户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "customerState", value = "1:独占客户 2:共享客户 3:成单客户 5:所有独占客户", paramType = "Integer"),
            @ApiImplicitParam(name = "lableIds", value = "标签id", paramType = "Integer[]"),
            @ApiImplicitParam(name = "name", value = "客户名", paramType = "String"),
            @ApiImplicitParam(name = "cardId", value = "员工Id", paramType = "Integer"),
            @ApiImplicitParam(name = "type", value = "1个人数据2企业数据", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true, company = true,card = true,paramClass = CustomerRequest.class)
    @PostMapping("getCustomerPC")
    public ResultModel<Map<String, Object>> getCustomerPC(@RequestAttribute CustomerRequest request) {
        
        return crmFegin.getCustomerPC(request);
    }

    @ApiOperation(value = "导出客户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "customerState", value = "1:独占客户 2:共享客户 3:成单客户", paramType = "Integer"),
            @ApiImplicitParam(name = "lableIds", value = "标签id", paramType = "Integer[]"),
            @ApiImplicitParam(name = "name", value = "客户名", paramType = "String"),
            @ApiImplicitParam(name = "type", value = "1个人数据2企业数据", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true, company = true,card = true,paramClass = CustomerRequest.class)
    @RequestMapping("getCustomerExport")
    public void getCustomerExport(@RequestAttribute CustomerRequest request, HttpServletResponse response) {
        ResultModel<ExportDataDTO<CustomerExcelEntity>> resultModel= crmFegin.getCustomerExport(request);
        if(resultModel.getCode().equals(ResultEnum.SUCCESS.getCode())){

            ExportDataDTO file=resultModel.getData();
            try{
                ExcelUtil.exportExcel(file, CustomerExcelEntity.class,response);
            }catch (Exception e){
                log.error("导出客户列表[异常]", e);
            }
        }
    }

    @ApiOperation(value = "导入客户数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "except文件", required = true, paramType = "MultipartFile"),
    })
    @ApiController(isLogin = true, company = true, card = true, isParam = false)
    @RequestMapping("importCustomer")
    public ResultModel<Boolean> importCustomer(MultipartFile file, @RequestAttribute TokenRequest tokenRequest) {
        // 创建处理EXCEL的类
        ImportUtil importUtil = new ImportUtil();
        // 解析excel
        List<Map<String, Object>> customerList = importUtil.getExcelInfo(file);
        if(customerList != null) {
            // 插入数据
            CustomerRequest request = new CustomerRequest();
            BeanUtils.copyProperties(tokenRequest, request);
            request.setCustomerList(customerList);
            return crmFegin.insertImportCustomer(request);
        }
        return ResultModel.fail();
    }

    @ApiOperation(value = "获取客户信息")
    @ApiImplicitParam(name = "cardId", value = "名片id", required = true, paramType = "Integer")
    @ApiController(isLogin = true, company = true,card = true,paramClass = BusinessCardRequest.class)
    @PostMapping("getCustomerInfo")
    public ResultModel<GetCustomerInfoResponse> getCustomerInfo(@RequestAttribute BusinessCardRequest request) {
        return crmFegin.getCustomerInfo(request.getCardId().longValue());
    }
    @ApiOperation(value = "客户列表")
    @ApiController(isLogin = true, company = true,card = true,paramClass = CustomerRequest.class)
    @PostMapping("getCustomerList")
    public ResultModel<List<CustomerResponse>> getCustomerList(@RequestAttribute CustomerRequest request) {
        
        return crmFegin.getCustomerList(request);
    }

    @ApiOperation(value = "成交记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "customerId", value = "客户id", required = true, paramType = "Integer")
    })
    @PostMapping("getInputDealList")
    @ApiController(isLogin = true, company = true,card = true,paramClass = InputDealMoneyRequest.class)
    public ResultModel<List<InputDealMoneyResponse>> getInputDealList(@RequestAttribute InputDealMoneyRequest request) {
        
        return crmFegin.getInputDealList(request);
    }

    @PostMapping("inputDealMoney")
    @ApiOperation(value = "录入成交金额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inputDealId", value = "录入表id", paramType = "Integer"),
            @ApiImplicitParam(name = "customerId", value = "客户id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "money", value = "录入金额", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "remark", value = "录入备注", required = true, paramType = "String"),
            @ApiImplicitParam(name = "photos", value = "录入图片", required = true, paramType = "String"),
    })
    @ApiController(isLogin = true, company = true,card = true,paramClass = InputDealMoneyRequest.class)
    public ResultModel<Boolean> inputDealMoney(@RequestAttribute InputDealMoneyRequest request) {
        return crmFegin.inputDealMoney(request);
    }

    @PostMapping("delInputDealPhotos")
    @ApiOperation(value = "删除成交记录表图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inputDealId", value = "录入成交记录表id", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true, company = true,card = true,paramClass = InputDealMoneyRequest.class)
    public ResultModel<Boolean> delInputDealPhotos(@RequestAttribute InputDealMoneyRequest request) {
        return crmFegin.delInputDealPhotos(request);
    }

    @ApiOperation(value = "手动创建客户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "电话", required = true, paramType = "String"),
            @ApiImplicitParam(name = "name", value = "姓名", required = true, paramType = "String"),
            @ApiImplicitParam(name = "personalWx", value = "个人微信", paramType = "String"),
            @ApiImplicitParam(name = "companyWx", value = "企业微信", paramType = "String"),
            @ApiImplicitParam(name = "email", value = "邮箱", paramType = "String"),
            @ApiImplicitParam(name = "position", value = "职位", paramType = "String"),
            @ApiImplicitParam(name = "companyId", value = "公司id", required = true, paramType = "String"),
            @ApiImplicitParam(name = "customerRemark", value = "备注", paramType = "String"),
            @ApiImplicitParam(name = "lableIds", value = "标签", required = true, paramType = "Integer[]"),
            @ApiImplicitParam(name = "customerState", value = "1:独占客户 2:共享客户", required = true, paramType = "String"),
            @ApiImplicitParam(name = "turnoverAmount", value = "成交金额", paramType = "String"),
            @ApiImplicitParam(name = "remark", value = "成交金额备注", paramType = "String"),
    })
    @ApiController(isLogin = true, company = true,card = true,paramClass = CustomerRequest.class)
    @PostMapping("manualAdd")
    public ResultModel<Boolean> manualAdd(@RequestAttribute CustomerRequest request) {
        return crmFegin.manualAdd(request);
    }

    @ApiOperation(value = "修改客户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerId", value = "客户id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "cardId", value = "名片id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "phone", value = "电话", required = true, paramType = "String"),
            @ApiImplicitParam(name = "name", value = "姓名", required = true, paramType = "String"),
            @ApiImplicitParam(name = "personalWx", value = "个人微信", paramType = "String"),
            @ApiImplicitParam(name = "companyWx", value = "企业微信", paramType = "String"),
            @ApiImplicitParam(name = "email", value = "邮箱", paramType = "String"),
            @ApiImplicitParam(name = "position", value = "职位", paramType = "String"),
            @ApiImplicitParam(name = "companyId", value = "公司id", paramType = "String"),
            @ApiImplicitParam(name = "createCompanyName", value = "公司名称", paramType = "String"),
            @ApiImplicitParam(name = "customerRemark", value = "备注", paramType = "String"),
            @ApiImplicitParam(name = "lableIds", value = "标签", paramType = "Integer[]"),
            @ApiImplicitParam(name = "customerState", value = "1:独占客户 2:共享客户 3:成单客户", required = true, paramType = "String"),
            @ApiImplicitParam(name = "turnoverAmount", value = "成交金额", paramType = "String"),
            @ApiImplicitParam(name = "remark", value = "成交金额备注", paramType = "String"),
    })
    @ApiController(isLogin = true, company = true,card = true,paramClass = CustomerRequest.class)
    @PostMapping("updCustomer")
    public ResultModel<Boolean> updCustomer(@RequestAttribute CustomerRequest request) {
        return crmFegin.updCustomer(request);
    }

    @ApiOperation(value = "修改客户标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerId", value = "客户id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "lableIds", value = "标签id", required = true, paramType = "Integer[]")
    })
    @PostMapping("updCustomerLabel")
    @ApiController(isLogin = true, company = true,card = true,paramClass = CustomerRequest.class)
    public ResultModel<String> updCustomerLabel(@RequestAttribute CustomerRequest request) {
        
        return crmFegin.updCustomerLabel(request);
    }

    @ApiOperation(value = "修改客户共享范围（抢为独占）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerIds", value = "客户id", required = true, paramType = "String"),
            @ApiImplicitParam(name = "customerState", value = "1:独占客户 2:共享客户", required = true, paramType = "Integer")
    })
    @PostMapping("updBatchCustomerState")
    @ApiController(isLogin = true, company = true,card = true,paramClass = CustomerRequest.class)
    public ResultModel<String> updBatchCustomerState(@RequestAttribute CustomerRequest request) {
        
        return crmFegin.updBatchCustomerState(request);
    }

    @ApiOperation(value = "添加标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lableColor", value = "标签颜色", paramType = "Long"),
            @ApiImplicitParam(name = "lableName", value = "标签名", paramType = "Long"),
            @ApiImplicitParam(name = "parentId", value = "父级id 为0表示自己就是父级", paramType = "Long"),
    })
    @ApiController(isLogin = true,company=true,card = true,paramClass = CompanyCustomerLableRequest.class)
    @PostMapping("addLable")
    public ResultModel<Long> addLable(@RequestAttribute CompanyCustomerLableRequest request) {
        
        return crmFegin.addLable(request);
    }

    @ApiOperation(value = "添加标签List")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lableId", value = "标签id", paramType = "Integer"),
            @ApiImplicitParam(name = "lableColor", value = "标签颜色", paramType = "Long"),
            @ApiImplicitParam(name = "lableName", value = "标签名", paramType = "Long"),
            @ApiImplicitParam(name = "list", value = "子标签list", paramType = "list"),
    })
    @ApiController(isLogin = true,company=true,card = true,isBoss = true,paramClass = CompanyCustomerLableRequest.class)
    @PostMapping("addLableList")
    public ResultModel<Boolean> addLableList(@RequestAttribute CompanyCustomerLableRequest request) {

        return crmFegin.addLableList(request);
    }

    @ApiOperation(value = "删除标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lableId", value = "标签id", paramType = "Long"),
    })
    @ApiController(isLogin = true,company=true,card = true,paramClass = CompanyCustomerLableRequest.class)
    @PostMapping("delLable")
    public  ResultModel<Boolean> delLable(@RequestAttribute CompanyCustomerLableRequest request) {
        
        return crmFegin.delLable(request);
    }

    @ApiOperation(value = "获取标签")
    @ApiController(isLogin = true,company=true,card = true,paramClass = CompanyCustomerLableRequest.class)
    @PostMapping("getTree")
    public  ResultModel<List<CompanyCustomerLableResponse>> getTree(@RequestAttribute CompanyCustomerLableRequest request) {
        
        return crmFegin.getTree(request);
    }

    @ApiOperation(value = "修改标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lableId", value = "标签id", paramType = "Long"),
            @ApiImplicitParam(name = "lableColor", value = "标签颜色", paramType = "Long"),
            @ApiImplicitParam(name = "lableName", value = "标签名", paramType = "Long"),
            @ApiImplicitParam(name = "parentId", value = "标签父级id", paramType = "Long"),
    })
    @ApiController(isLogin = true,company=true,card = true,paramClass = CompanyCustomerLableRequest.class)
    @PostMapping("updLable")
    public  ResultModel<Boolean> updLable(@RequestAttribute CompanyCustomerLableRequest request) {
        return crmFegin.updLable(request);
    }

    @PostMapping("moveCustomer")
    @ApiOperation(value = "企业员工移交客户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerIds", value = "客户Id,逗号隔开", required = true, paramType = "String"),
            @ApiImplicitParam(name = "ascriptionId", value = "客户移交后归属人cardId", paramType = "Integer"),
            @ApiImplicitParam(name = "customerState", value = "1:独占客户，2:共享客户", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true, company = true, card = true,paramClass = CustomerRequest.class)
    public ResultModel moveCustomer(@RequestAttribute CustomerRequest request) {
        
        return crmFegin.moveCustomer(request);
    }

    @ApiOperation(value = "添加跟进记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cardId", value = "跟进用户名片id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "followType", value = "跟进方式 1电话拜访 ,2微信拜访,3手动添加,4当面拜访,5其它", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "followRemark", value = "跟进备注",  paramType = "Integer"),
    })
    @PostMapping("addFollow")
    @ApiController(isLogin = true,company=true,card = true,paramClass = FollowRecordRequest.class)
    public ResultModel<Boolean> addFollow(@RequestAttribute FollowRecordRequest request) {
        
        return crmFegin.addFollow(request);
    }

    @ApiOperation(value = "获取跟进记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cardId", value = "跟进用户名片id", paramType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, paramType = "Integer"),
    })
    @PostMapping("getFollowList")
    @ApiController(isLogin = true,company=true,card = true,paramClass = FollowRecordRequest.class)
    public ResultModel<List<FollowRecordResponse>> getFollowList(@RequestAttribute FollowRecordRequest request) {
        
        return crmFegin.getFollowList(request);
    }
}
